package cn.stylefeng.guns.modular.cb.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.cb.entity.CbAboutUs;
import cn.stylefeng.guns.modular.cb.mapper.CbAboutUsMapper;
import cn.stylefeng.guns.modular.cb.model.params.CbAboutUsParam;
import cn.stylefeng.guns.modular.cb.model.result.CbAboutUsResult;
import  cn.stylefeng.guns.modular.cb.service.CbAboutUsService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wj
 * @since 2021-02-02
 */
@Service
public class CbAboutUsServiceImpl extends ServiceImpl<CbAboutUsMapper, CbAboutUs> implements CbAboutUsService {

    @Override
    public void add(CbAboutUsParam param){
        CbAboutUs entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CbAboutUsParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CbAboutUsParam param){
        CbAboutUs oldEntity = getOldEntity(param);
        CbAboutUs newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CbAboutUsResult findBySpec(CbAboutUsParam param){
        return null;
    }

    @Override
    public List<CbAboutUsResult> findListBySpec(CbAboutUsParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CbAboutUsParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 关于我们
     * @return
     */
    @Override
    public CbAboutUsResult getAboutUs() {
        return this.baseMapper.getAboutUs();
    }

    private Serializable getKey(CbAboutUsParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CbAboutUs getOldEntity(CbAboutUsParam param) {
        return this.getById(getKey(param));
    }

    private CbAboutUs getEntity(CbAboutUsParam param) {
        CbAboutUs entity = new CbAboutUs();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
